import {Component, OnInit} from '@angular/core';
import {Constants} from "../../../../../tools";
import {FormControl, FormGroup} from "@angular/forms";
import {BarnDocument} from "../entity/barn-document";
import {BarnProduct} from "../../product/entity/barn-product";
import {ProductService} from "../../product/service/product.service";
import {BarnDocumentService} from "../service/barn-document.service";
import {NzMessageService} from "ng-zorro-antd/message";
import {PicUploadComponent} from "../../../../common-components/pic-upload/pic-upload/pic-upload.component";
import {Router} from "@angular/router";

@Component({
  selector: 'app-all-document',
  templateUrl: './all-document.component.html',
  styleUrls: ['./all-document.component.less']
})
export class AllDocumentComponent implements OnInit {

  releaseYes = Constants.Enable.YES;
  releaseNo = Constants.Enable.NO

  imgDownloadUrl = PicUploadComponent.DOWNLOAD_URL

  validateForm = new FormGroup({
    documentTitle: new FormControl(''),
    productId: new FormControl(''),
    isRelease: new FormControl("")
  })

  loading = false

  documentList: BarnDocument[] = []
  allProducts: BarnProduct[] = []

  total = 0;
  current = 1;
  pageSize = 20

  constructor(private productService: ProductService, private documentService: BarnDocumentService, private router: Router) {
  }

  ngOnInit(): void {
    const param = new BarnProduct();
    param.enable = Constants.Enable.YES
    this.productService.query(param).subscribe(value => {
      this.allProducts = value.data
    })
    this.search()
  }

  search() {
    this.loading = true;
    const param = this.validateForm.getRawValue() as BarnDocument;
    param.isRelease = this.releaseYes
    this.documentService.query(param, this.current, this.pageSize).subscribe(value => {
      this.loading = false
      this.current = value.current || 1;
      this.total = value.count || 1;
      this.documentList = value.data;
    }, () => this.loading = false)
  }

  resetForm() {
    this.validateForm.reset({isRelease: "", productId: ""})
  }

  showDocument(pid: string) {
    this.router.navigate(['/welcome/blog/query/preview'],{ queryParams:{pid}})
  }

  pageChange(index){
    this.current = index
    this.documentList= []
    this.search()
  }

}
